package org.yty.travelagencymongodb.service.impl;

import cn.hutool.core.util.IdUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.yty.travelagencymongodb.model.Entity.SignUp;
import org.yty.travelagencymongodb.model.Entity.TourGroup;
import org.yty.travelagencymongodb.model.Entity.Traveller;
import org.yty.travelagencymongodb.model.VO.SignUpQueryByTourGroupIdVO;
import org.yty.travelagencymongodb.model.VO.SignUpQueryByTravellerIdVO;
import org.yty.travelagencymongodb.repository.SignUpRepository;
import org.yty.travelagencymongodb.service.SignUpService;
import org.yty.travelagencymongodb.service.TourGroupService;
import org.yty.travelagencymongodb.service.TravellerService;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class SignUpServiceImpl implements SignUpService {
    private final SignUpRepository signUpRepository;
    @Lazy
    @Autowired
    private TravellerService travellerService;
    @Lazy
    @Autowired
    private TourGroupService tourGroupService;
    private final MongoTemplate mongoTemplate;

    @Override
    public SignUpQueryByTourGroupIdVO getTravellerListByTourGroupId(Long tourGroupId) {
        Criteria queryCriteria = Criteria.where("tourGroupId").is(tourGroupId);
        List<SignUp> signUpList = this.mongoTemplate.find(new Query(queryCriteria), SignUp.class);
        List<Long> travellerIdList = signUpList.stream().map(elem -> elem.getTravellerId()).toList();
        List<Traveller> travellerList = this.travellerService.getListByIdList(travellerIdList);
        return new SignUpQueryByTourGroupIdVO(signUpList, travellerList);
    }

    @Override
    public SignUpQueryByTravellerIdVO getTourGroupListByTravellerId(Long travellerId) {
        List<SignUp> signUpList = this.mongoTemplate.find(new Query(Criteria.where("travellerId").is(travellerId)), SignUp.class);
        List<Long> tourGroupIdList = signUpList.stream().map(elem -> elem.getTourGroupId()).toList();
        List<TourGroup> tourGroupList = this.tourGroupService.getListByIdList(tourGroupIdList);
        return new SignUpQueryByTravellerIdVO(signUpList, tourGroupList);
    }

    @Override
    public void deleteByTourGroupIdAndTravellerId(Long tourGroupId, Long travellerId) {
        this.signUpRepository.deleteByTourGroupIdAndTravellerId(tourGroupId, travellerId);
    }

    @Override
    public boolean save(SignUp signUp) {
        if (signUp.getId() == null){
            signUp.setId(IdUtil.getSnowflakeNextId());
        }
        signUp.setCreateTime(LocalDateTime.now());
        signUp.setUpdateTime(LocalDateTime.now());
        signUp = this.signUpRepository.insert(signUp);
        return signUp != null;
    }

    @Override
    public boolean update(SignUp signUp) {
        signUp.setUpdateTime(LocalDateTime.now());
        signUp = this.signUpRepository.save(signUp);
        return signUp != null;
    }

    @Override
    public void deleteByTravellerId(Long travellerId) {
        this.signUpRepository.deleteByTravellerId(travellerId);
    }

    @Override
    public void deleteByTourGroupId(Long tourGroupId) {
        this.signUpRepository.deleteByTourGroupId(tourGroupId);
    }
}
